/*
 * Rule.cc
 *
 *  Created on: Jun 29, 2012
 *      Author: victor
 */

#include "Rule.h"

Rule::Rule(){
	commands = new vector< Command*>();
	pipedCommand = new PipedCommand();
	lastCommand = 0;
	newPipe = false;
}



void Rule::addCommand(Command* newCommand){
	if(newPipe){
		if(!commands->empty()){
			pipedCommand->addCommand(lastCommand);
			commands->erase(commands->end()-1);
			newPipe = false;
		}
	} else {
		commands->push_back(newCommand);
	}
	lastCommand = newCommand;
}

void Rule::addPipe(){
	newPipe = true;
}

void Rule::executeCommands(){

	if (pipedCommand->hasCommands()){
		pipedCommand->executeCommands(true);
	} else {
		for(vector<Command*>::iterator i = commands->begin(); i!= commands->end(); ++i){
			(*i)->execute();
		}
	}
}

void Rule::clear(){
	for(vector<Command*>::iterator i = commands->begin(); i!= commands->end(); ++i){
		delete (*i);
	}
	commands->clear();
}

Command * Rule::getLastCommand(){
	return lastCommand;
}

void Rule::removeLastCommand(){
	//TODO: implement this
}
